<template>
  <div class="ChildPage TablePage" v-loading="loading">

    <vb-page ref="page" @onTableSelection="doSelect">
      <template #headerBtn="scope">
        <el-button size="small" type="success"  icon="el-icon-share" @click="uploadKingDeeBill(this.selection)">批量上传金蝶</el-button>
      </template>
      <template #table-handle-prepend="scope">
        <el-button size="mini" type="text" icon="el-icon-edit" @click="gotoDetail(scope.row.billNo, scope.row.stockNo)">查看</el-button>
        <el-button size="mini" type="text" style="color:var(--el-color-success)" icon="el-icon-share" @click="toExport(scope.row.billNo, scope.row.stockNo)">导出</el-button>
      </template>

      <template #table-stockNo="scope">
        {{ getStockName(scope.row.stockNo) }}
      </template>
      <template #table-count="scope">
        {{ scope.row.count? Number(scope.row.count.toFixed(2)): scope.row.count }}
      </template>
      <template #table-status="scope">
        <el-tag :type="scope.row.status === 'save'? 'info': 'success'">{{ scope.row.status === 'save'? '保存': '审核' }}</el-tag>
      </template>
    </vb-page>
  </div>
</template>

<script>
export default {
  data() {
    return {
      materialLoading: false,
      reloadMaterial: false,
      materialList: [],
      selection: null,
      selectionLength:0,
      selectCount:0
    }
  },
  async mounted() {
    await this.getStock()
  },
  methods: {
    doSelect(data) {
      this.selection = data
      this.selectionLength = this.selection.length;
      this.selectCount = 0
      for (var i = 0; i < this.selectionLength; i++) {
        this.selectCount += data[i].count
      }
      this.selectCount = parseFloat(this.selectCount).toFixed(2)
    },
    async uploadKingDeeBill() {
      if (!this.selection || this.selection.length === 0) {
        this.$message.error('没有选择行')
        return
      }
      // this.$refs.selectRowStockIn.open(this.doPrdIn)
      this.doPrdIn(this.selection)
    },
    async doPrdIn(form) {
      console.log(form)
      let billNoList = []
      for(let i = 0; i < this.selection.length; i++) {
        if (this.selection[i].status === 'audit') {
          billNoList.push(this.selection[i].billNo)
        }
      }
      try {
        await this.$confirm(`单据(${ billNoList.length }个)将被执行上传金蝶单据, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
      } catch(e) {
        return
      }
      this.loading = true
      try {
        let res = await this.callApi('upload_kingdee_bill', {
          billNoList: billNoList,
          stockNo: form.stockNo,
          locationNo: form.locationNo
        })
        if (!res.res) {
          return
        }
        this.$message.success(`上传完成!`)
        return true;
      } finally {
        this.loading = false
      }
    },
    async getMaterialList(val) {
      if (!val) {
        this.materialList = []
        return
      }
      this.materialLoading = true
      try {
        const res = await this.callApi('get_material_by_key', { key: val, pageSize: 20 })
        if (!res.res) return
        this.materialList = res.data
      } finally {
        this.materialLoading = false
      }
    },
    toExport(billNo,stockNo) {
      window.open(import.meta.env.VITE_EXPORT_URL + `saleOutExport?billNo=${ billNo}&stockNo=${stockNo}`)
    },
    async getStock() {
      this.loading = true;
      try {
        let res = await this.$api.getChildDictionaryListByKeys({
          keys: ['stock']
        });
        if (!res.res) return;
        this.stockList = res.stock;
      } finally {
        this.loading = false;
      }
    },
    /**
     * 获取状态名
     */
    getStockName(val) {
      if (this.stockList && this.stockList.length > 0) {
        for(let i = 0; i < this.stockList.length; i++) {
          if (this.stockList[i].key === val) {
            return this.stockList[i].name;
          }
        }
      }
      return '';
    },
    gotoDetail(billNo = '', stockNo = '') {
      let url = '/sale/jkySaleBillDetail'
      if (billNo && stockNo) {
        url += `?billNo=${ billNo }&stockNo=${stockNo}`
      }
      this.$router.push(url)
    }
  }
}
</script>

<style scoped>

</style>
